package service.brandServiceImpl;

import mapper.brandMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.Brand;
import service.brandService.brandService;
import util.factory;

import java.util.List;

public class brandServiceImpl implements brandService {
    //获取工厂
    factory f=new factory();
     SqlSessionFactory sqlSessionFactory=f.getSqlSessionFactory();
    @Override
    public List<Brand> selectAll() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        List<Brand> brands = mapper.selectAll();//mapper去调用brandMapper中的方法
        sqlSession.close();//释放资源
        return brands;
    }

    @Override
    public void add(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        mapper.add(brand);//mapper去调用brandMapper中的方法
        sqlSession.commit();//增删改记得提交事务
        sqlSession.close();//释放资源
    }

    @Override
    public void deleteById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        mapper.deleteById(id);//mapper去调用brandMapper中的方法
        sqlSession.commit();//增删改记得提交事务
        sqlSession.close();//释放资源
    }

    @Override
    public Brand selectById(int id) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        Brand brand = mapper.selectById(id);//mapper去调用brandMapper中的方法
        sqlSession.commit();//释放资源
        return brand;
    }

    @Override
    public void update(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        mapper.update(brand);//mapper去调用brandMapper中的方法
        sqlSession.commit();//提交事务
        sqlSession.close();//释放资源
    }

    @Override
    public void deleteByIds(int[] ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        mapper.deleteByIds(ids);//mapper去调用brandMapper中的方法
        sqlSession.commit();//提交事务
        sqlSession.close();//释放资源
    }

    @Override
    public List<Brand> selectByPage(int currentPage, int pageSize) {
        //计算起始索引，和最大显示页数,作为sql方法的参数
        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        List<Brand> rows = mapper.selectByPage(begin, size);//mapper去调用brandMapper中的方法
        sqlSession.close();//释放资源
        return rows;

    }

    @Override
    public int selectTotal() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        int totalCount = mapper.selectTotal();//mapper去调用brandMapper中的方法
        sqlSession.close();//释放资源
        return totalCount;
    }

    @Override
    public List<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //计算起始索引，和最大显示页数,作为sql方法的参数
        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        List<Brand> brands = mapper.selectByPageAndCondition(begin, size, brand);//mapper去调用brandMapper中的方法
        sqlSession.close();//释放资源
        return brands;
    }

    @Override
    public int selectTotalByCondition(Brand brand) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        brandMapper mapper = sqlSession.getMapper(brandMapper.class);
        int total = mapper.selectTotalByCondition(brand);//mapper去调用brandMapper中的方法
        sqlSession.close();//释放资源
        return total;
    }
}
